import os
import sys
from PIL import Image


class FormatConversion:
	def __init__(self):
		print('>>>Loading...')
		self.make_png_dir()
		self.make_tiff_dir()
		self.make_jpeg_dir()
		self.make_webp_dir()
		self.make_icon_dir()
		self.make_original_dir()
		print('>>>OK')
		os.system('cls' if os.name == 'nt' else 'clear')

	@staticmethod
	def get_image_name(image_path='./OriginalImage') -> list:
		"""
		获取指定目录内的文件名称列表
		:param image_path: 指定目录
		:return: 文件列表
		"""
		file_list = os.listdir(image_path)
		return file_list

	@staticmethod
	def make_original_dir() -> None:
		if not os.path.exists('./OriginalImage'):
			print('>>>Creating dir:`OriginalImage`...')
			os.mkdir('./OriginalImage')

	@staticmethod
	def make_webp_dir() -> None:
		if not os.path.exists('./WEBP'):
			print('>>>Creating dir:`WEBP`...')
			os.mkdir('./WEBP')

	@staticmethod
	def make_jpeg_dir() -> None:
		if not os.path.exists('./JPEG'):
			print('>>>Creating dir:`JPEG`...')
			os.mkdir('./JPEG')

	@staticmethod
	def make_tiff_dir() -> None:
		if not os.path.exists('./Tiff'):
			print('>>>Creating dir:`TIFF`...')
			os.mkdir('./Tiff')

	@staticmethod
	def make_png_dir() -> None:
		if not os.path.exists('./PNG'):
			print('>>>Creating dir:`PNG`...')
			os.mkdir('./PNG')

	@staticmethod
	def make_icon_dir() -> None:
		if not os.path.exists('./Icon'):
			print('>>>Creating dir:`ICON`...')
			os.mkdir('./Icon')

	@staticmethod
	def to_jpeg(image_list: list) -> bool:
		try:
			for i in image_list:
				img = Image.open('./OriginalImage/' + i)
				i = i.split('.')[0] + '.JPEG'
				img.save('./JPEG/' + i, 'JPEG')
			print(f'>>>The format conversion was successful.[./JPEG]')
			return True
		except Exception:
			return False

	@staticmethod
	def to_png(image_list: list) -> bool:
		try:
			for i in image_list:
				img = Image.open('./OriginalImage/' + i)
				i = i.split('.')[0] + '.PNG'
				img.save('./PNG/' + i, 'PNG')
			print(f'>>>The format conversion was successful.[./PNG]')
			return True
		except Exception:
			return False

	@staticmethod
	def to_tiff(image_list: list) -> bool:
		try:
			for i in image_list:
				img = Image.open('./OriginalImage/' + i)
				i = i.split('.')[0] + '.TIFF'
				img.save('./TIFF/' + i, 'TIFF')
			print(f'>>>The format conversion was successful.[./TIFF]')
			return True
		except Exception:
			return False

	@staticmethod
	def to_webp(image_list: list) -> bool:
		try:
			for i in image_list:
				img = Image.open('./OriginalImage/' + i)
				i = i.split('.')[0] + '.WEBP'
				img.save('./WEBP/' + i, 'WEBP')
			print(f'>>>The format conversion was successful.[./WEBP]')
			return True
		except Exception:
			return False

	@staticmethod
	def to_icon(image_list: list) -> bool:
		try:
			for i in image_list:
				img = Image.open('./OriginalImage/' + i)
				i = i.split('.')[0] + '.ICO'
				size = min(img.size)
				square_img = Image.new('RGBA', (size, size), (0, 0, 0, 0))
				square_img.paste(img, ((size-img.size[0])//2, (size-img.size[1])//2))
				square_img.save('./Icon/' + i, 'ICO')
			print(f'>>>The format conversion was successful.[./ICON]')
			return True
		except Exception:
			return False

	def run(self):
		print('-' * 5 + 'Supported formats' + '-' * 5)
		print('1.JPEG')
		print('2.TIFF')
		print('3.PNG')
		print('4.WEBP')
		print('5.ICON')
		export_format = input('>>>Please input the export format(1~4): ')
		original_images = self.get_image_name()
		if export_format == '1':
			return self.to_jpeg(original_images)
		elif export_format == '2':
			return self.to_tiff(original_images)
		elif export_format == '3':
			return self.to_png(original_images)
		elif export_format == '4':
			return self.to_webp(original_images)
		elif export_format == '5':
			return self.to_icon(original_images)


if __name__ == '__main__':
	format_conversion = FormatConversion()
	if format_conversion.run():
		input('>>>ENTER TO EXIT')
		sys.exit(0)
	else:
		input('>>>Error: unknown error!')
